package com.gao.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gao.weblog.common.domain.dos.ArticleDO;
import com.gao.weblog.common.domain.dos.ArticlePublishCountDO;
import com.gao.weblog.common.model.dto.ArticleQueryDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

public interface ArticleMapper extends BaseMapper<ArticleDO> {


    default Page<ArticleDO> selectPageList(ArticleQueryDTO articleQueryDTO) {
        Long current = articleQueryDTO.getCurrent();
        Long size = articleQueryDTO.getSize();

        Page<ArticleDO> page = new Page<>(current, size);

        String title = articleQueryDTO.getSearchTitle();
        LocalDate startDate = articleQueryDTO.getStartDate();
        LocalDate endDate = articleQueryDTO.getEndDate();

        LambdaQueryWrapper<ArticleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), ArticleDO::getTitle, title)
                .ge(Objects.nonNull(startDate), ArticleDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), ArticleDO::getCreateTime, endDate)
                .orderByDesc(ArticleDO::getCreateTime);

        return selectPage(page, wrapper);

    }

    default List<ArticleDO> selectAllReadNum() {
        return selectList(Wrappers.<ArticleDO>lambdaQuery().select(ArticleDO::getReadNum));
    }

    @Select("SELECT TRUNC(create_time) AS date, COUNT(*) AS count\n" +
            "FROM weblog.t_article\n" +
            "WHERE create_time >= #{startDate} AND create_time < #{endDate}\n" +
            "GROUP BY TRUNC(create_time)")
    List<ArticlePublishCountDO> selectDateArticlePublishCount(
            @Param("startDate") LocalDate startDate,
            @Param("endDate")   LocalDate endDate
    );

    default int increaseReadNum(Long articleId) {
        return update(null, Wrappers.<ArticleDO>lambdaUpdate()
                .setSql("read_num = read_num + 1")
                .eq(ArticleDO::getId, articleId));
    }

    default Page<ArticleDO> selectPageListByArticleIds(Long current, Long size, List<Long> articleIds){
        Page<ArticleDO> page = new Page<>(current, size);
        LambdaQueryWrapper<ArticleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ArticleDO::getId, articleIds)
                .orderByDesc(ArticleDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    default ArticleDO selectPreArticle(Long articleId) {
        LambdaQueryWrapper<ArticleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ArticleDO::getId)
                .gt(ArticleDO::getId, articleId)
                .last("limit 1");
        return selectOne(wrapper);
    }

    default ArticleDO selectNextArticle(Long articleId) {
        LambdaQueryWrapper<ArticleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ArticleDO::getId)
                .lt(ArticleDO::getId, articleId)
                .last("limit 1");
        return selectOne(wrapper);
    }

}
